package com.maotai.llzhou.timetask;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.annotation.Resource;

import org.apache.http.client.config.RequestConfig;

import Decoder.BASE64Encoder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.bean.MaotaiString;
import com.maotai.llzhou.bean.MaotaiWebCookie;
import com.maotai.llzhou.register.MaotaiRegisterLoginThread;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.util.HttpClientUtilNew;
import com.maotai.llzhou.util.IPFactory;
import com.maotai.llzhou.util.JSUtil;

/**
 * 注册
 */
public class MaotaiRegisterSGTask {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;
	private Scanner scanner;
	

	public void beginTask() {
		try {
			try {
			
				MaotaiWebCookie mwc = new MaotaiWebCookie();
				BASE64Encoder encode = new BASE64Encoder();
				HttpClientUtilNew hcu = HttpClientUtilNew.getInstance();
				System.out.print("输入手机号：");
				scanner = new Scanner(System.in);
				String telphone = scanner.nextLine();
				System.out.println("输入的手机号为："+telphone); 
				
				//发送验证码到手机
				String send_url = MaotaiString.SEND_MOBILE_URL.replace("xxx", telphone);
//				JSONArray jArray = IPFactory.getIpAndPort();
//				JSONObject jj = (JSONObject) jArray.get(0);
//				String ipAndPort = jj.getString("ip") + ":"
//						+ jj.getString("port");
				JSONArray jArray = IPFactory.getIpAndPort();
				JSONObject jj = (JSONObject) jArray.get(0);
				String ipAndPort = jj.getString("ip") + ":"
						+ jj.getString("port");
				RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
				mwc = hcu.sendHttpGetProxy(send_url, mwc, config, MaotaiString.REGISTER_REFERER_URL);
				//{"data":"发送成功","resultCode":0,"resultMsg":"success"}
				System.out.println(mwc.getContent());
				//接收验证码
				String pwd = encode.encode(("a"+telphone).getBytes());
				pwd = pwd.replace("=", "");
				System.out.print("输入接收到的验证码：");
				String telCode = scanner.nextLine();
				System.out.println("输入的验证码为："+telCode); 
				Map<String, String> registerMap = new HashMap<String, String>();
				registerMap.put("userName", telphone);
				registerMap.put("password", pwd);
				registerMap.put("mobile", telphone);
				registerMap.put("verifyCode", telCode);
				registerMap.put("confimpwd", pwd);
				registerMap.put("appCode", "newrretail");
				registerMap.put("channel", "B2C");
				mwc = hcu.sendHttpPostProxy(MaotaiString.REGISTER_URL, mwc, registerMap ,config,MaotaiString.REGISTER_REFERER_URL);
				
				System.out.println(mwc.getContent());
				//{"data":null,"resultCode":-1,"resultMsg":"验证码错误"}
				JSONObject jObject = JSONObject.parseObject(mwc.getContent());
				if(jObject!=null && "您的手机已注册".equals(jObject.getString("retmsg"))){
					System.out.println("手机号已注册");
				}else if(jObject!=null && "success".equals(jObject.getString("retmsg"))){
					System.out.println("手机号未注册，等待获取验证码....");
				}else{
					Map<String,String> map = new HashMap<String,String>();
					map.put("code", telphone);
					map.put("pwd", "a"+telphone);
					map.put("phone", telphone);
					maotaiService.insertZhanghaoWeb(map);
					
					Map<String,Object> zhanghao = new HashMap<String,Object>();
					zhanghao.put("code", telphone);
					zhanghao.put("pwd", pwd);
					zhanghao.put("phone", telphone);
//					String timestamp = System.currentTimeMillis()+"";
//					String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
//					mwc.setSign(sign);
//					mwc.setTimestamp(timestamp);
//					MaotaiRegisterLoginThread mrl = new MaotaiRegisterLoginThread(maotaiService, zhanghao, ipAndPort);
//					mrl.run();
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
